import 'package:containerbox_study/routers/router_table.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

// ignore: camel_case_types
class articleItem extends StatelessWidget {
  final String title;
  final String imageUrl;
  final int viewCount; //查看次数
  final int index;


  const articleItem(this.title, this.imageUrl, this.viewCount, this.index,{Key? key})
      : super(key: key);

  static const double Margin_Size = 10;
  static const double TitleHeight = 50;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
      margin: EdgeInsets.all(Margin_Size),
      child: Row(
        children: [
          Expanded(
            child: Column(
            children: [
              _titleWraper(context, this.title),
              _viewCountWraper(this.viewCount.toString())
            ],
          )),
          _imageviewWraper(),
          Container(
            margin: EdgeInsets.fromLTRB(10, 0, 0, 10),
            height: 1,
            color: Colors.grey,
            child: Divider(
              color: Colors.grey,
              height: 1,
            ),
          )
        ],
      ),
    ),

    onTap: () async {
      print('点击列表 $title');
      // Map <String, String> arguments = {'title' : title};
      //跳转, 并且传参, 返回时 将参数带回来(回传)
      // var argument = await Navigator.of(context).pushNamed(RouterTable.articleDetailPath, arguments: arguments);

      //跳转并且传参
      // Navigator.of(context).pushNamed(RouterTable.articleDetailPath, arguments: arguments);

      // print('返回数据 $argument');
      //从二级页面回传过来的数据
      // ScaffoldMessenger.of(context).showSnackBar(
      //   SnackBar(content: Text('返回 ${(argument as Map<String, dynamic>)['title']}')),
      // );

      //使用第三方 路由插件
      // RouterManager.dynamicDetailPath
      RouterManager.router!.navigateTo(context, '${RouterManager.articleDetailPath}/${this.title}');

    },
    );
  }

  Widget _titleWraper(BuildContext context, String text) {
    return Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.fromLTRB(Margin_Size, 10, 0, 0),
      height: TitleHeight,
      child: Text(
        this.title,
        maxLines: 2,
        textAlign: TextAlign.left,
      ),
    );
  }

  Widget _viewCountWraper(String text) {
    return Container(
      margin: EdgeInsets.fromLTRB(Margin_Size, 0, 0, 0),
      child: Row(
        children: [
          Icon(
            Icons.remove_red_eye_outlined,
            size: 15,
            color: Colors.grey,
          ),
          Container(
            margin: EdgeInsets.fromLTRB(Margin_Size, 0, 0, 0),
            child: Text(
              this.viewCount.toString(),
              style: TextStyle(color: Colors.grey, fontSize: 15,),
            ),
          )
        ],
      ),
    );
  }

  Widget _imageviewWraper() {
    return Container(
        margin: EdgeInsets.fromLTRB(Margin_Size, 0, 0, 0),
        height: 80,
        width: 110,
        child: CachedNetworkImage(
          imageUrl: this.imageUrl,
          fit: BoxFit.fill,
          progressIndicatorBuilder: (context, url, progress) =>
              LinearProgressIndicator(
            value: progress.progress,
          ),
        ));
  }
}
